package com.tx.txlibs;

import android.content.Context;
import android.nfc.Tag;

import com.google.gson.Gson;


public class LockSdkCls {
    private static final String TAG = "TxLockSdkCls";

    private static final LockSdkCls LOCK_SDK_CLS = new LockSdkCls();
    private static Context context = null;


    /**
     * Used to load the LockSdk-lib' library on application startup.
     *
     * @return
     */
    public static LockSdkCls getInstance(Context context) {

        LockSdkCls.context = context;
        MUR500Cls.setContext(context);
        NfcReaderCls.setContext(context);
        return LOCK_SDK_CLS;
    }

    // Used to load the 'LockSdk-lib' library on application startup.
    static {
        System.loadLibrary("LockSdk");
    }

    private LockSdkCls() {
    }


    // 有卡片靠近的时候, 请刷新Tag
    public void setTagContext(Tag tag, Context context) {
        // this.tag = tag;
        NfcReaderCls.setTagContext(tag, context);
        this.context = context;
    }


///////////////////////////////////////////////////////////////////////////////////////
    public Result cardIssueJson(String jsonStr) {
        Gson gson = new Gson();

        CardInfo info = gson.fromJson(jsonStr, CardInfo.class);
        return jniCardIssue(info.type, info.clientCode, info.lockNumList, info.buildingList, info.floorList,
                info.areaList, info.beginTime, info.endTime, info.period, info.lostCardID, info.flags, info.replaceNo, info.amount);
    }

    /*public Result cardIssue(int type, String clientCode, String lockNumList, String buildingList
            , String floorList, String areaList, String beginTime, String endTime, String period, String lostCardID, int flags, int replaceNo, int amount) {
        return jinJiangCardIssue(type, clientCode, lockNumList, buildingList, floorList, areaList, beginTime, endTime, period, lostCardID, flags, replaceNo, amount);
    }*/

    public String cardRead() {

        CardInfo info = jniCardRead();
//        Result result = new Result();
//
        Gson gson = new Gson();
//        result.jsonData = gson.toJson(info);
//        result.cardID = info.cardID;
        return gson.toJson(info);
    }

    public Result cardCancel() {

        return jniCardCancel();
    }

    public Result readSystemPas() {
        return jniReadSystemPas();
    }


    public void beepOk() {
        jniBeepOk();
    }

    public int readCardSn(byte[] cardSn) {
        return jniReadCardSn(cardSn);
    }

    public int TpConfig(Context activityContext) {
        return jniTpConfig(activityContext);
    }

    public int ClrConfig() {
        return jniClrConfig();
    }

    /////////////////// JNI 的native 方法 ////////////////////////////////
    private native Result jniCardIssue(int type, String clientCode, String lockNumList, String buildingList, String floorList, String areaList, String beginTime, String endTime, String period, String lostCardID, int flags, int replaceNo, int amount);

    private native CardInfo jniCardRead(); // 读卡信息

    private native Result jniCardCancel(); // 这本地函数也属于当前类, 只不过实现代码在C库里面.

    private native Result jniReadSystemPas(); // 读取授权卡, 可以不调用此函数

    private native void jniBeepOk(); // BeepOK

    private native int jniReadCardSn(byte[] cardSn); // 读取卡号

    private native int jniTpConfig(Context activityContext); // SDK配置

    private native int jniClrConfig(); // 清除读卡器里面的SDK配置信息
}
